package com.ngu.biz.notepad.services.Impl;

import com.ngu.biz.notepad.dao.NotepadsMapper;
import com.ngu.biz.notepad.model.Notepads;
import com.ngu.biz.notepad.services.NotepadsService;
import com.ngu.util.AjaxResponse;
import com.ngu.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Description:    记事本逻辑层
* @Author:         hq
* @CreateDate:     2020/2/7
*/
@Service
public class NotepadsServiceImpl implements NotepadsService {

    @Autowired
    private NotepadsMapper notepadsMapper;

    /**
     * @Description:    记事本条件查询
     * @Author:         hq
     * @CreateDate:     2020/2/7
     */
    @Override
    public AjaxResponse selectNotepadsList(Notepads record) {
        //处理分页
        record.setPage((record.getPage() - 1)* record.getLimit());
        record.setLimit(record.getLimit());
        //调用查询数据接口
        List<Notepads> data = notepadsMapper.selectNotepadsList(record);
        //调用返回数据总条数接口
        Integer count = notepadsMapper.selectNotepadsListCount(record);
        return AjaxResponse.pageResponse("列表查询成功！",data,count,record.getPage());
    }

    /**
     * @Description:    记事本新增
     * @Author:         hq
     * @CreateDate:     2020/2/7
     */
    @Override
    public AjaxResponse addNotepads(Notepads notepads) {
       notepads.setnCreateTime(TimeUtils.getNowTime());
       int data = notepadsMapper.insertSelective(notepads);
       if(data == 1){
        return  AjaxResponse.success("记事本新增成功！","200",data);
       }else{
        return   AjaxResponse.error("记事本新增失败！","500",data);
       }
    }

    /**
     * @Description:    记事本修改
     * @Author:         hq
     * @CreateDate:     2020/2/7
     */
    @Override
    public AjaxResponse updateNotepads(Notepads notepads) {
        notepads.setnUpdateTime(TimeUtils.getNowTime());
        int data = notepadsMapper.updateByPrimaryKeySelective(notepads);
        if(data == 1){
            return  AjaxResponse.success("记事本修改成功！","200",data);
        }else{
            return   AjaxResponse.error("记事本修改失败！","500",data);
        }
    }
    /**
     * @Description:    记事本删除
     * @Author:         hq
     * @CreateDate:     2020/2/7
     */
    @Override
    public AjaxResponse deleteNotepadsById(Notepads notepads) {
        int data = notepadsMapper.deleteByPrimaryKey(notepads.getnId());
        if(data == 1){
            return  AjaxResponse.success("记事本删除成功！","200",data);
        }else{
            return   AjaxResponse.error("记事本删除失败！","500",data);
        }
    }

    @Override
    public AjaxResponse deleteNotepadsByIds(String nIds) {
        // 记录删除多少条数据
        int dataNum = 0;
        // 拿到所有记事本的id， 可能有多个id 用逗号拼接，所以用逗号去进行截取，再用数组 装起来。
        String[] list = nIds.split(",");
        // 通过循环去 拿到每个id 然后去 根据id逐条 请求接口删除(还是调用原来的之前单条删除的dao层的方法)。
        for (String id:list) {
            dataNum += notepadsMapper.deleteByPrimaryKey(Long.valueOf(id));
        }
        if(dataNum >= 1){
            return  AjaxResponse.success("记事本删除+"+dataNum+"+条数据成功！","200",dataNum);
        }else{
            return   AjaxResponse.error("记事本删除失败！","500",dataNum);
        }
    }

}
